//
//  LocationTableViewCell.m
//  ChineseCollegeStudentsNetwork
//
//  Created by Bazinga on 2018/8/10.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "LocationTableViewCell.h"

@interface LocationTableViewCell ()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UIView *divisionView;
@property (nonatomic, strong) UIButton *defaultButton;
@property (nonatomic, strong) UIButton *compileButton;
@property (nonatomic, strong) UIButton *deleteButton;
@end
@implementation LocationTableViewCell
#pragma mark - Init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
        [self setupLayout];
    }
    return self;
}
#pragma mark- UI
- (void)setupUI {
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    
    self.phoneLabel = [[UILabel alloc] init];
    self.phoneLabel.textColor = UIColorFromRGBHex(0x666666);
    self.phoneLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    
    self.addressLabel = [[UILabel alloc] init];
    self.addressLabel.textColor = UIColorFromRGBHex(0x666666);
    self.addressLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    self.addressLabel.numberOfLines = 0;
    
    self.divisionView = [[UIView alloc] init];
    self.divisionView.backgroundColor = UIColorFromRGBHex(0x666666);
    
    self.defaultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.defaultButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.defaultButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    self.defaultButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.defaultButton setTitle:@"  设为默认地址" forState:UIControlStateNormal];
    [self.defaultButton setImage:[UIImage imageNamed:@"ad_n"] forState:UIControlStateNormal];
    [self.defaultButton setImage:[UIImage imageNamed:@"ad_s"] forState:UIControlStateSelected];
    [[self.defaultButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_clickBlock) {
            _clickBlock(1,_curModel);
        }
    }];
    
    self.compileButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.compileButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.compileButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.compileButton setTitle:@"编辑" forState:UIControlStateNormal];
    [[self.compileButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_clickBlock) {
            _clickBlock(2,_curModel);
        }
    }];

    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [[self.deleteButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_clickBlock) {
            _clickBlock(3,_curModel);
        }
    }];

}

- (void)setupLayout{
    [self addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-30);
    }];
    
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(10);
    }];
    
    
    [self addSubview:self.defaultButton];
    [self.defaultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.width.offset(140);
        make.height.offset(30);
        make.bottom.offset(-10);
    }];
    
    [self addSubview:self.deleteButton];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.offset(-10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];
    
    [self addSubview:self.compileButton];
    [self.compileButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.deleteButton.mas_left).offset(-10);
        make.bottom.offset(-10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];
    
    [self addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(20);
        make.right.mas_equalTo(self.phoneLabel);
        make.bottom.equalTo(self.compileButton.mas_top).offset(-30);
    }];
    
    [self addSubview:self.divisionView];
    [self.divisionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.compileButton.mas_top).offset(-15);
        make.height.mas_equalTo(.5);
    }];


}

- (void)setCurModel:(AddressListModel *)curModel {
    _curModel = curModel;
    self.nameLabel.text = _curModel.realName;
    self.phoneLabel.text = _curModel.mobile;
    self.addressLabel.text = [NSString stringWithFormat:@"%@  %@",_curModel.location,_curModel.address];
    
    if (_curModel.isDefault.integerValue == 1) {
        self.defaultButton.selected = YES;
    } else {
        self.defaultButton.selected = NO;
    }
}


@end
